package csp;

import java.util.*;

public class SafetyIndex {
    public static int show_1(int[][] val, int num, int m) {
        int t = val[num][0];
        int sum = 0;

        for (int i = 0; i < m; i++) {
            if ((val[i][0] >= t && val[i][1] == 1) || (val[i][0] < t && val[i][1] == 0)) {
                sum++;
            }
        }

        return sum;
    }

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        int m;
        int result;
        int[][] sample;
        int[][] process;
        int t;

        m = input.nextInt();
        sample = new int[m][2];
        process = new int[m][2];

        for (int i = 0; i < m; i++) {
            sample[i][0] = input.nextInt();
            sample[i][1] = input.nextInt();
            process[i][0] = sample[i][0];
        }

        for (int i = 0; i < m; i++) {
            process[i][1] = show_1(sample, i, m);
        }

        for (int i = 0; i < m; i++) {
            System.out.println("val:" + process[i][0] + ", num:" + process[i][1]);
        }

        Arrays.sort(process, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                if(o2[1] == o1[1]){
                    return o2[0] - o1[0];
                }else {
                    return o2[1] - o1[1];
                }
            }
        });

        System.out.println();
        for (int i = 0; i < m; i++) {
            System.out.println("val:" + process[i][0] + ", num:" + process[i][1]);
        }

        result = process[0][0];
        System.out.println("那个阈值为：" + result);
    }
}
